'use strict';

const Service = require('egg').Service;

class BookService extends Service {
    async page(query) {
        const { ctx, app } = this
        const { Op } = app.Sequelize
        const { page, limit } = query
        const where = { upt_act: { [Op.ne]: 'D' } };
        if (query.book_num) {
            where.book_num = { [Op.like]: `%${query.book_num}%` };
        }
        if (query.book_name) {
            where.book_name = { [Op.like]: `%${query.book_name}%` };
        }
        if (query.writer) {
            where.writer = { [Op.like]: `%${query.writer}%` };
        }
        if (query.publisher) {
            where.publisher = { [Op.like]: `%${query.publisher}%` };
        }
        if (query.site) {
            where.site = query.site
        }
        if (query.isBorrow) {
            if (query.isBorrow == '可借阅') {
                where.amount = { [Op.gt]: 0, }
            } else {
                where.amount = 0
            }
        }
        return await ctx.model.Book.findAndCountAll({
            distinct: true, // 不加distinct，count和实际不符
            // include: [
            //     {
            //         model: ctx.model.Sys.User,
            //         attributes: ['id', 'username', 'name'],
            //     },
            // ],
            include: {
                model: ctx.model.Borrow,
                attributes: ['id', 'username', 'state'],
                // where: {
                //     state : { [Op.or]: [ '0', ' ' ] }
                // }
            },
            where,
            offset: (page - 1) * limit,
            limit: parseInt(limit),
        })
    }
    async create(body) {
        // 如果一本书已经存在的话，则把它的数量加1就好
        const { ctx } = this
        const res = await ctx.model.Book.findOne({
            where: {
                book_num:body.book_num
            }
        })
        if(res) {
            return await ctx.model.Book.update({amount:res.amount + body.amount},{
                where: {
                    book_num:body.book_num
                }
            })
        }
        // 如果这本书不存在，则把它添加到库存中
        try {
            return await ctx.model.Book.create(body)
        } catch (error) {
            console.log(error);
            return false
        }
    }
    async update(body) {
        const { ctx } = this
        body.upt_act = 'U'
        body.updated_at = ctx.helper.formatTime(new Date());
        try {
            const res = await ctx.model.Book.update(body, {
                where: { book_num: body.book_num }
            })
            //这里要注意返回的是一个数组
            return { success: res[0] }
        } catch (error) {
            console.log(error);
            return { success: false }
        }
    }
    async delete(body) {
        const { ctx } = this
        body.upt_act = 'D'
        body.updated_at = ctx.helper.formatTime(new Date());
        try {
            const res = await ctx.model.Book.update(body, {
                where: { book_num: body.book_num }
            })
            return { success: res[0] }
        } catch (error) {
            console.log(error);
            return { success: false }
        }
    }
    async getUserInfoByBook(body) {
        const { ctx } = this
        console.log(body);
        try {
            return await ctx.model.Borrow.findAndCountAll({
                where: {
                    book_num: body.book_num,
                    state: '0'
                },
                attributes: ['username', 'book_num'],
                include: {
                    model: ctx.model.Sys.User,
                    attributes: ['name', 'sex', 'phone'],
                    // include: {
                    //     model: ctx.model.Sys.Role,
                    //     attributes: ['name'],
                    // },
                },
            })
        } catch (error) {
            console.log(error);
            return false
        }
    }
}
module.exports = BookService;